<template>
  <div class="test-ai-container">
    <h1>AI助手测试页面</h1>
    <div class="test-section">
      <h3>测试AI连接状态</h3>
      <button @click="testAIConnection" :disabled="testing">
        {{ testing ? '测试中...' : '测试AI连接' }}
      </button>
      <div v-if="testResult" class="test-result" :class="{ success: testSuccess, error: !testSuccess }">
        {{ testResult }}
      </div>
    </div>
    
    <div class="test-section">
      <h3>发送测试消息</h3>
      <div class="input-group">
        <input 
          v-model="testMessage" 
          placeholder="输入测试消息"
          @keyup.enter="sendTestMessage"
        />
        <button @click="sendTestMessage" :disabled="sendingTest">
          {{ sendingTest ? '发送中...' : '发送测试' }}
        </button>
      </div>
      <div v-if="testResponse" class="test-response">
        <strong>AI回复：</strong>
        <p>{{ testResponse }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { chatWithSpark } from '@/api/sparkService.js';

const testing = ref(false);
const testResult = ref('');
const testSuccess = ref(false);
const testMessage = ref('你好，我是测试用户');
const sendingTest = ref(false);
const testResponse = ref('');

const testAIConnection = async () => {
  testing.value = true;
  testResult.value = '';
  
  try {
    const startTime = Date.now();
    const response = await fetch('/spark', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer rFanHQUBkEOUbZnjUOoB:tzOSJAWvogCYsgkbXkcW'
      },
      body: JSON.stringify({
        model: 'x1',
        messages: [{ role: 'user', content: '测试连接' }],
        user: '10284711用户',
        stream: false,
        temperature: 0.7,
        max_tokens: 50
      })
    });
    
    const endTime = Date.now();
    const responseTime = endTime - startTime;
    
    if (response.ok) {
      testSuccess.value = true;
      testResult.value = `✅ API连接成功！响应时间：${responseTime}ms`;
    } else {
      testSuccess.value = false;
      testResult.value = `❌ API连接失败：${response.status} ${response.statusText}`;
    }
  } catch (error) {
    testSuccess.value = false;
    testResult.value = `❌ 网络错误：${error.message}`;
  } finally {
    testing.value = false;
  }
};

const sendTestMessage = async () => {
  if (!testMessage.value.trim()) return;
  
  sendingTest.value = true;
  testResponse.value = '';
  
  try {
    const messages = [
      { role: 'user', content: testMessage.value }
    ];
    
    const responses = await chatWithSpark(messages);
    testResponse.value = responses[0] || '没有收到回复';
  } catch (error) {
    testResponse.value = `错误：${error.message}`;
  } finally {
    sendingTest.value = false;
  }
};
</script>

<style scoped>
.test-ai-container {
  max-width: 600px;
  margin: 50px auto;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.test-section {
  margin-bottom: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.test-section h3 {
  margin-top: 0;
  color: #333;
}

button {
  padding: 10px 20px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
}

button:hover:not(:disabled) {
  background: #0056b3;
}

button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.test-result {
  margin-top: 15px;
  padding: 10px;
  border-radius: 5px;
  font-size: 14px;
}

.test-result.success {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.test-result.error {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.input-group {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.input-group input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.test-response {
  margin-top: 15px;
  padding: 15px;
  background: #e9ecef;
  border-radius: 5px;
  border-left: 4px solid #007bff;
}

.test-response p {
  margin: 10px 0 0 0;
  line-height: 1.5;
}
</style>